package edu.ccut.saturn.manager.bundles.core;

import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;

public class QuerySysAppBundles extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		
		List<SaturnData> bundleList = (ArrayList<SaturnData>)parameterContext.getParameterValue(0);
		List<SaturnData> sysBundleList = new ArrayList<SaturnData>(); 
		List<SaturnData> appBundleList = new ArrayList<SaturnData>(); 
		
		for(int i=0;i<bundleList.size();i++){
			SaturnData bundleInfo = (SaturnData)bundleList.get(i);
			String bundleFilePath = bundleInfo.getString("bundleFilePath");
			
			if(bundleFilePath != null) {
				if(bundleFilePath.indexOf("\\plugins") != -1) {
					appBundleList.add(bundleInfo);
				}else if(bundleFilePath.indexOf("\\1") != -1 
						|| bundleFilePath.indexOf("\\2") != -1) {
					sysBundleList.add(bundleInfo);
			    }else if(bundleInfo.getString("key").equals("System Bundle")){
			    	sysBundleList.add(bundleInfo);
				}else{
					appBundleList.add(bundleInfo);
				}		
			}	
		}

		parameterContext.setParameter(1, sysBundleList);
		parameterContext.setParameter(2, appBundleList);
	}
}